package com.example.demo.redis测试.事务;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @projectName:demo
 * @see:com.example.demo.redis测试.事务
 * @author:袁成龙
 * @createTime:2020/10/16 13:22
 * @version:1.0
 */
@RestController
public class RedisMulti {

    @Autowired
    private RedisTemplate redisTemplate;

    public void test(){
        redisTemplate.multi();

        //执行redis 操作命令   先入队queue


        redisTemplate.exec();
    }

    
    /*
     * @Author 袁成龙
     * @Description //TODO 管道技术和普通操作的对比,这个是普通的方式插入  13.231秒
     * @Date 14:57 2020/10/16
     * @Param 
     * @return 
     **/
    @RequestMapping("putong")
    public void putong(){
        long l = System.currentTimeMillis();
        for (int i = 0; i <100000 ; i++) {
            redisTemplate.opsForValue().set("lisi"+i,"listi"+i);
        }
        long l1 = System.currentTimeMillis();

        System.out.println((l1-l)/1000.0);
    }



    /**
     * @Author 袁成龙
     * @Description //TODO 管道测试插入redis  3.305秒
     * @Date 10:32 2020/10/18
     * @Param 
     * @return 
     **/
    @RequestMapping("guandao")
    public void guandao(){
        long l = System.currentTimeMillis();
        redisTemplate.executePipelined(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
                //打开通道
                redisConnection.openPipeline();
                for (int i = 0; i <100000 ; i++) {
                    redisConnection.set(("lisi"+i).getBytes(),("listi"+i).getBytes());
                //    redisTemplate.opsForValue().set("lisi"+i,"listi"+i);
                }
                //关闭通道
                redisConnection.closePipeline();
                return null;
            }
        });

        long l1 = System.currentTimeMillis();

        System.out.println((l1-l)/1000.0);
    }


    /*
     * @Author 袁成龙
     * @Description //TODO 在优化提高速率   1.854,如果数据量更大 比如千万级别的可以用管道+这种的优化
     * @Date 14:57 2020/10/16
     * @Param
     * @return
     **/
    @RequestMapping("putongyouhua")
    public void putongyouhua(){
        long l = System.currentTimeMillis();
        Map<String,String> map = new HashMap<>();
        for (int i = 0; i <100000 ; i++) {
            map.put("lisi"+i,"listi"+i);
        }
        redisTemplate.opsForValue().multiSet(map);
        long l1 = System.currentTimeMillis();

        System.out.println((l1-l)/1000.0);
    }






    /**
     * @Author 袁成龙
     * @Description //TODO 管道测试插入redis  管道优化    1.537
     * @Date 10:32 2020/10/18
     * @Param
     * @return
     **/
    @RequestMapping("guandaoyouhua")
    public void guandaoyouhua(){
        long l = System.currentTimeMillis();
        redisTemplate.executePipelined(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
                //打开通道
                Map<byte[],byte[]> map = new HashMap<>();
                redisConnection.openPipeline();
                for (int i = 0; i <100000 ; i++) {
                   // redisConnection.set(("lisi"+i).getBytes(),("listi"+i).getBytes());
                    //    redisTemplate.opsForValue().set("lisi"+i,"listi"+i);
                    map.put(("lisi"+i).getBytes(),("listi"+i).getBytes());
                }
                redisConnection.mSet(map);
                //关闭通道
                redisConnection.closePipeline();
                return null;
            }
        });

        long l1 = System.currentTimeMillis();

        System.out.println((l1-l)/1000.0);
    }


    public static void main(String[] args) {

    }

}